<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单信息</title>
    <script src="../js/vue.global.js"></script>
    <script src="../js/vue-router.global.js"></script>
    <script src="../js/axios.min.js"></script>


    <style>
        /* 全局样式 */
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
        }

        #app {
            max-width: 1000px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
        }

        h1 {
            color: #2c3e50;
            border-bottom: 3px solid #3498db;
            padding-bottom: 10px;
            margin-bottom: 30px;
        }

        h2 {
            color: #34495e;
            font-size: 1.2em;
            margin: 25px 0 15px;
        }

        /* 按钮样式 */
        button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s ease;
            margin: 5px;
        }

        button:hover {
            background-color: #2980b9;
            transform: translateY(-1px);
        }

        /* 输入框样式 */
        input[type="text"] {
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 5px;
            width: 300px;
            margin-right: 10px;
            transition: border-color 0.3s;
        }

        input[type="text"]:focus {
            outline: none;
            border-color: #3498db;
        }

        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #ecf0f1;
        }

        th {
            background-color: #3498db;
            color: white;
            font-weight: bold;
        }

        tr:hover {
            background-color: #f8f9fa;
        }

        tr:nth-child(even) {
            background-color: #f8f9fa;
        }

        /* 删除按钮特殊样式 */
        button[data-action="delete"] {
            background-color: #e74c3c;
        }

        button[data-action="delete"]:hover {
            background-color: #c0392b;
        }
    </style>
</head>
<body>


    <h1>订单信息</h1>
    <div id="app">
        <h2>查询当前用户所有订单</h2>
        <button @click="getOrders">查询所有订单</button> <br/>

        <h2>根据商品名称查询订单</h2>
        <input type="text" v-model="productName" placeholder="请输入商品名称"></input>
        <button @click="getOrdersByProductName">查询</button> <br/>

        <h2>订单列表</h2>
        <table>
            <thead>
                <tr>
                    <th>订单名称</th>
                    <th>订单金额</th>
                    <th>删除订单</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="product in ordersProducts">
                    <td>{{product.name}}</td>
                    <td>{{product.price}}</td>
                    <td>
                        <button @click="deleteOrder(product.id)">删除</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

<script>
    const app=Vue.createApp({
        data(){
            return{
                ordersProducts:[],
                productName:''
            }
        },
        methods:{
            getOrders(){
                console.log('getOrders');
                axios.get('/orders/list')
               .then(response=>{
                    this.ordersProducts=response.data;
                    console.log(this.ordersProducts);
                })
               .catch(error=>{
               })
            },
            getOrdersByProductName(){
                console.log('getOrdersByProductName');
                axios.get('/orders/searchByName?productName='+this.productName)
               .then(response=>{
                    this.ordersProducts=response.data;
                    console.log(this.ordersProducts);
               })
               .catch(error=>{
                   console.log(error);
               })
            },
            deleteOrder(id){
                console.log('deleteOrder');
                axios.delete(`/orders/${id}`)
                .then(response=>{
                    console.log(response.data);
                    this.getOrders();
                })
                .catch(error=>{
                   console.log(error);
                })
            }
        }
    })

    app.mount('#app')
</script>
</body>
</html>